﻿using netDxf;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Tables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestDxfDocument

{

    class Class10
    {
        public static void TriangularInsert(Dictionary<string, Object> data)    //正三边形刀片，适用于T形刀片
        {
            DxfDocument dxf = new DxfDocument();




            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer center = new Layer("center") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            /*
            double tic = Convert.ToDouble(data["ic"]);
            double scale = Convert.ToDouble(data["scale"]); //放大系数
            if (scale == 0 && tic < 9)
            {
                scale = 3.0;
            }
            else if (scale == 0 && tic < 20)
            {
                scale = 2.5;
            }
            else if (scale == 0)
            {
                scale = 2.0;
            }
            */

            string path = Convert.ToString(data["path"]);
            double pi = Math.PI;

            double IC = 9.525;
            double S = 3.175;
            double L = 16.5;
            double M = 2.45;
            double BS = 2.0;
            double EPSR = 60;
            double KRINS = 90;
            double KCH = EPSR/2;
            double AN = 11;
            double BCH = 0.3;
            double AS = 20 ;

            double AS_1= AS * (Math.PI / 180);
            double KCH_1 = KCH * (Math.PI / 180);
            double EPSR_1 = EPSR * (Math.PI / 180);
            double KRINS_1 = KRINS * (Math.PI / 180);
            double EPSRN = 30 * (Math.PI / 180);
            double An_1 = AN * (Math.PI / 180);
            


            Vector2 p1 = new Vector2(0, 0);
            Vector2 p2 = new Vector2((BCH * (Math.Cos(90 - KRINS)) + BS * (Math.Sin(KCH_1))) / Math.Tan(EPSR_1), BCH * (Math.Cos(90 - KRINS)) + BS * (Math.Sin(KCH_1)));
            Vector2 p3 = new Vector2(p2.X + BCH * (Math.Cos(KRINS_1)), p2.Y - BCH * (Math.Cos(90 - KRINS)));

            Vector2 p4 = new Vector2(p3.X + (BS * Math.Cos(KCH_1)), p1.Y);
            Vector2 p5 = new Vector2(IC / Math.Tan(EPSR_1 / 2), p1.Y);
            Vector2 p6 = new Vector2((IC / 2) / Math.Tan(EPSR_1 / 2), IC / Math.Tan(EPSR_1 / 2) * Math.Sin(EPSR_1));
            Vector2 p7 = new Vector2(p6.X, IC / 2);

            double length1 = fun.CalDistance(p1, p2);
            double length2 = fun.CalDistance(p1, p3);
            double length3 = fun.CalDistance(p1, p4);
            Vector2 p8 = new Vector2(p6.X - Math.Sin(EPSRN) * length1, p6.Y - Math.Cos(EPSRN) * length1);
            Vector2 p9 = new Vector2(p6.X, p6.Y - length2);
            Vector2 p10 = new Vector2(p6.X + Math.Sin(EPSRN) * length1, p8.Y);
            Vector2 p11 = new Vector2(p5.X - Math.Cos(EPSR_1) * length1, p5.Y + Math.Sin(EPSR_1) * length1);
            Vector2 p12 = new Vector2(p11.X, p11.Y - BCH);
            Vector2 p13 = new Vector2(p5.X - length3, p5.Y);

            Vector2 p14 = new Vector2(p5.X + 15, p5.Y);
            Vector2 p15 = new Vector2(p14.X, p14.Y + L);
            Vector2 p16 = new Vector2(p15.X - S, p15.Y - S * Math.Tan(An_1));
            Vector2 p17 = new Vector2(p14.X - S, p14.Y + S * Math.Tan(An_1));

            Vector2 p18 = new Vector2(p17.X - 5, p17.Y + 8);
            Vector2 p19 = new Vector2(p18.X - 2 * BS * Math.Sin(KCH_1), p18.Y + 2 * BS * Math.Cos(KCH_1));
            Vector2 p20 = new Vector2(p19.X - S * Math.Cos(KCH_1), p19.Y - S * Math.Sin(KCH_1));
            Vector2 p21 = new Vector2(p20.X + 2 * BS* Math.Sin(KCH_1), p20.Y - 2 * BS * Math.Cos(KCH_1));

            Vector2 p22 = new Vector2(p7.X - IC / 2, p7.Y);
            Vector2 p23 = new Vector2(p7.X + IC / 2, p7.Y);
            Vector2 p24 = new Vector2(p13.X, p13.Y - 3);
            Vector2 p25 = new Vector2(p14.X - 4, p14.Y);

            Vector2 p26 = new Vector2(p1.X - 2.87, p1.Y - 4);
            Vector2 p27 = new Vector2(p7.X + IC * Math.Cos(KCH_1), p7.Y - IC * Math.Sin(KCH_1));
            Vector2 p28 = new Vector2(p12.X, p12.Y + 10);

            double angular2 = fun.CalAngle(p11, p12);


            Vector2[] Line1 = { p2, p3 };
            fun.drawBLine(dxf, Line1, 0.3, contour);
            Vector2[] Line2 = { p4, p3 };
            fun.drawBLine(dxf, Line2, 0.3, contour);
            Vector2[] Line3 = { p2, p8 };
            fun.drawBLine(dxf, Line3, 0.3, contour);

            Vector2[] Line7 = { p8, p9 };
            fun.drawBLine(dxf, Line7, 0.3, contour);
            Vector2[] Line8 = { p10, p9 };
            fun.drawBLine(dxf, Line8, 0.3, contour);
            Vector2[] Line9 = { p11, p12 };
            fun.drawBLine(dxf, Line9, 0.3, contour);
            Vector2[] Line10 = { p12, p13 };
            fun.drawBLine(dxf, Line10, 0.3, contour);
            Vector2[] Line11 = { p4, p13 };
            fun.drawBLine(dxf, Line11, 0.3, contour);
            Vector2[] Line12 = { p10, p11 };
            fun.drawBLine(dxf, Line12, 0.3, contour);


            LwPolyline Arc00 = new LwPolyline();
            Arc00.Vertexes.Add(new LwPolylineVertex(p1));
            Arc00.Vertexes.Add(new LwPolylineVertex(p5));
            Arc00.Vertexes.Add(new LwPolylineVertex(p6));
            Arc00.Vertexes.Add(new LwPolylineVertex(p1));
            Arc00.Layer = additionalline;
            dxf.AddEntity(Arc00);

            /*
            Vector2[] Line13 = { p6, p8 };
            fun.drawBLine(dxf, Line13, 0.3, contour);
            Vector2[] Line14 = { p6, p10 };
           fun.drawBLine(dxf, Line14, 0.3, contour);
 
            Vector2[] Line5 = { p5, p11 };
          fun.drawBLine(dxf, Line5, 0.3, contour);
            Vector2[] Line6 = { p5, p13 };
            fun.drawBLine(dxf, Line6, 0.3, contour);
            Vector2[] Line4 = { p1, p2 };
           fun.drawBLine(dxf, Line4, 0.3, contour);
           Vector2[] Line16= { p1, p4 };
           fun.drawBLine(dxf, Line4, 0.3, contour);*/
            Vector2[] Line17 = { p14, p17 };
            fun.drawBLine(dxf, Line17, 0.3, contour);

            Vector2[] Line18 = { p16, p17 };
            fun.drawBLine(dxf, Line18, 0.3, contour);
            Vector2[] Line19 = { p16, p15 };
            fun.drawBLine(dxf, Line19, 0.3, contour);
            Vector2[] Line20 = { p14, p15 };
            fun.drawBLine(dxf, Line20, 0.3, contour);
            Vector2[] Line21 = { p18, p19 };
            fun.drawBLine(dxf, Line21, 0.3, contour);
            Vector2[] Line22 = { p18, p21 };
            fun.drawBLine(dxf, Line22, 0.3, contour);
            Vector2[] Line23 = { p20, p21 };
            fun.drawBLine(dxf, Line23, 0.3, contour);
           


            LwPolyline poly1 = new LwPolyline();
            poly1.Vertexes.Add(new LwPolylineVertex(p14));
            poly1.Vertexes.Add(new LwPolylineVertex(p15));
            poly1.Vertexes.Add(new LwPolylineVertex(p16));
            poly1.Vertexes.Add(new LwPolylineVertex(p17));
            poly1.Vertexes.Add(new LwPolylineVertex(p14));

            //            poly1.SetConstantWidth(0.05);
            poly1.IsClosed = true;
            fun.PolyFill(dxf, poly1, 0.4, contour);

            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = poly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = poly1.Explode()[i].Type.ToString();    //将所有元素的类型记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)poly1.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    harc1[i] = (Arc)poly1.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 45, 5, hline1, harc1, hatching);

            Circle circle00 = new Circle(p7, IC / 2);
            circle00.Layer = center;
            circle00.Lineweight = Lineweight.W50;
            circle00.Linetype = Linetype.DashDot;
            dxf.AddEntity(circle00);

            List<Vector3> points00 = new List<Vector3>
           {
                 new Vector3(p20.X,p20.Y , 0),
                new Vector3(p20.X+0.3,p20.Y+0.3, 0),
                new Vector3(p20.X+1.5,p20.Y+0.5, 0),
                new Vector3(p20.X+2.5,p20.Y+1.5, 0),
                  new Vector3(p19.X,p19.Y, 0),



            };

            Spline spline00 = new Spline(points00);
            spline00.Layer = contour;
            DxfDocument doc00 = new DxfDocument();
            dxf.AddEntity(spline00);

            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 0.2;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            //文本样式
            dimensionStyle.TextHeight = 0.3;
            double offset = 1;
            //线段尺寸标注
            //尺寸1
            LinearDimension dim1 = new LinearDimension(new Line(p1, p6), offset, 60, dimensionStyle);
            dim1.UserText = "l";
            dxf.AddEntity(dim1);

            LinearDimension dim2 = new LinearDimension(new Line(p22, p23), offset, 0, dimensionStyle);
            dim2.UserText = "d";
            dxf.AddEntity(dim2);
            LinearDimension dim3 = new LinearDimension(new Line(p10, p23), offset - 2, 180, dimensionStyle);
            dim3.UserText = "m";
            dxf.AddEntity(dim3);
            LinearDimension dim4 = new LinearDimension(new Line(p11, p12), offset - 4, 90, dimensionStyle);
            dim4.UserText = "bs";
            dxf.AddEntity(dim4);
            LinearDimension dim5 = new LinearDimension(new Line(p13, p12), offset + 3, 200, dimensionStyle);
            dim5.UserText = "bs";
            dxf.AddEntity(dim5);
            LinearDimension dim6 = new LinearDimension(new Line(p15, p16), offset + 2, 360, dimensionStyle);
            dim6.UserText = "S";
            dxf.AddEntity(dim6);
            LinearDimension dim7 = new LinearDimension(new Line(p13, p27), offset + 5, 125, dimensionStyle);
            dim7.UserText = "M";
            dxf.AddEntity(dim7);



            Angular2LineDimension angdim1 = new Angular2LineDimension(new Line(p5, p11), new Line(p5, p13), offset + 3, dimensionStyle);
            angdim1.UserText = "εr";
            dxf.AddEntity(angdim1);

            Angular2LineDimension angdim2 = new Angular2LineDimension(new Line(p13, p1), new Line(p13, p24), offset + 1, dimensionStyle);
            angdim2.UserText = "xr=90°";
            dxf.AddEntity(angdim2);
            if (AN == 11)
            {
                Angular2LineDimension angdim4 = new Angular2LineDimension(new Line(p14, p25), new Line(p14, p17), offset + 5, dimensionStyle);
                angdim4.UserText = "11°±1′";
                dxf.AddEntity(angdim4);
            }
            if (AN == 20)
            {
                Angular2LineDimension angdim4 = new Angular2LineDimension(new Line(p14, p25), new Line(p14, p17), offset + 5, dimensionStyle);
                angdim4.UserText = "20°±30′";
                dxf.AddEntity(angdim4);
            }


            if (AS==11)
            {
                Angular2LineDimension angdim5 = new Angular2LineDimension(new Line(p18, p12), new Line(p18, p21), offset + 5, dimensionStyle);
                angdim5.UserText = "11°±30′";
                dxf.AddEntity(angdim5);
            }
            if (AS == 20)
            {
                Angular2LineDimension angdim5 = new Angular2LineDimension(new Line(p18, p12), new Line(p18, p21), offset + 5, dimensionStyle);
                angdim5.UserText = "20°±1′";
                dxf.AddEntity(angdim5);
            }
            if (AS == 15)
            {
                Angular2LineDimension angdim5 = new Angular2LineDimension(new Line(p18, p12), new Line(p18, p21), offset + 5, dimensionStyle);
                angdim5.UserText = "15°±1′";
                dxf.AddEntity(angdim5);
            }
            Angular2LineDimension angdim3 = new Angular2LineDimension(new Line(p26, p8), new Line(p26, p12), offset + 17, dimensionStyle);
            angdim3.UserText = "φ";
            dxf.AddEntity(angdim3);
          
            Angular2LineDimension angdim6 = new Angular2LineDimension(new Line(p26, p8), new Line(p26, p12), offset + 17, dimensionStyle);
            angdim6.UserText = "φ";
            dxf.AddEntity(angdim6);
            Angular2LineDimension angdim7 = new Angular2LineDimension(new Line(p28, p1), new Line(p28, p12), offset + 17, dimensionStyle);
            angdim7.UserText = "φ";
            dxf.AddEntity(angdim7);



            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);









        }
    }
}
